package com.study.spark.scala.dataset

import org.apache.spark.sql.SparkSession

/**
 * select操作demo
 *
 * @author stephen
 * @date 2019-09-27 15:41
 */
object SelectDemo {

  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .appName("SelectDemo")
      .master("local[*]")
      .getOrCreate()

    // 隐式转换
    import spark.implicits._
    import org.apache.spark.sql.functions._

    val df = spark.createDataset(Seq((1,"zhangsan",20),(2,"lisi",30),(3,"wangwu",40)))
      .toDF("id","name","age")

    //选择列的几种调用方式
    df.select("name").show()
    // $"列名"这个是语法糖，返回Column对象
    df.select($"name").show()
    df.select(col("name")).show()

    // 转大写
    df.select(upper($"name")).show()
    df.selectExpr("upper(name) as upper_name","age as u_age").show()

    spark.stop()
  }
}
